package  
{
	import ad.media.SoundFacade;
	import ad.utils.ConversionUtil;
	
	import client.graphics.GRSeekbar;
	import client.graphics.GRBalloon;
	
	import com.greensock.easing.*;
	import com.greensock.TweenMax;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	
	/**
	 * MediaSound
	 * @version ActionScript 3.0
	 * @author Adrian C. Miranda
	 * @mail ad@adrianmiranda.com.br
	 */
	public class MediaSound extends Sprite
	{
		private var soundFacade:SoundFacade = new SoundFacade('./files/YouMakeMeFeel.mp3', true, false, true, 3000);
		private var seekbar:GRSeekbar = new GRSeekbar();
		private var balloon:GRBalloon = new GRBalloon();
		private var timestamp:GRBalloon = new GRBalloon();
		
		public function MediaSound()
		{
			super();
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
		}
		
		private function onAddedToStage(evt:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			draw();
			startSoundFacade();
		}
		
		private function draw():void
		{
			seekbar.x = 100;
			seekbar.y = 100;
			seekbar.addEventListener(MouseEvent.MOUSE_MOVE, onGRBalloonMouseMove, false, 0, true);
			seekbar.hit.buttonMode = true;
			seekbar.hit.addEventListener(MouseEvent.MOUSE_DOWN, onGRSeekbarMouseDown, false, 0, true);
			addChild(seekbar);
			
			addChild(balloon);
			
			timestamp.alignPointer = GRBalloon.TOP_CENTER;
			addChild(timestamp);
		}
		
		private function onGRBalloonMouseMove(evt:MouseEvent):void 
		{
			var distance:Number = ((evt.stageX - seekbar.x) / seekbar.width);
			var position:int = Math.floor(soundFacade.length * distance);
			var currentmin:int = int((position * 0.001) / 60);
			var currentseg:int = int((position * 0.001) % 60);
			balloon.text = String(timeFormat(currentmin) + ':' + timeFormat(currentseg)) + ' / ' + ((soundFacade.totalTime) ? soundFacade.totalTime : '00:00');
			balloon.x = seekbar.x + (distance * seekbar.width) - (balloon.width / 2);
			balloon.y = seekbar.y - balloon.height;
			TweenMax.to(balloon, 0.8, { alpha:1, ease:Expo.easeOut } );
			TweenMax.to(balloon, 1, { delay:1.5, alpha:0, ease:Expo.easeOut } );
		}
		
		private function timeFormat(value:int):String
		{
			return String(value < 10 ? '0' + value : value);
		}
		
		private function onGRSeekbarMouseDown(evt:MouseEvent):void 
		{
			onGRSeekbarMouseMove(evt);
			seekbar.hit.addEventListener(MouseEvent.MOUSE_MOVE, onGRSeekbarMouseMove, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp, false, 0, true);
		}
		private function onGRSeekbarMouseMove(evt:MouseEvent):void 
		{
			var distance:Number = ((evt.stageX - seekbar.x) / seekbar.width);
			var position:int = Math.floor(soundFacade.length * distance);
			if (seekbar.buffer.scaleX > distance)
			{
				soundFacade.stop();
				soundFacade.play(position);
			}
		}
		private function onStageMouseUp(evt:MouseEvent):void 
		{
			seekbar.hit.removeEventListener(MouseEvent.MOUSE_MOVE, onGRSeekbarMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		
		private function startSoundFacade():void
		{
			soundFacade.addEventListener(IOErrorEvent.IO_ERROR, onSoundFacadeIOError, false, 0, true);
			soundFacade.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSoundFacadeSecurityError, false, 0, true);
			soundFacade.addEventListener(Event.OPEN, onSoundFacadeOpen, false, 0, true);
			soundFacade.addEventListener(Event.ID3, onSoundFacadeLoadID3, false, 0, true);
			soundFacade.addEventListener(ProgressEvent.PROGRESS, onSoundFacadeProgress, false, 0, true);
			soundFacade.addEventListener(SoundFacade.PLAY_PROGRESS, onSoundFacadePlayProgress, false, 0, true);
			soundFacade.addEventListener(Event.COMPLETE, onSoundFacadeComplete, false, 0, true);
			soundFacade.addEventListener(Event.SOUND_COMPLETE, onSoundFacadeSoundComplete, false, 0, true);
		}
		
		private function onSoundFacadeIOError(evt:IOErrorEvent):void 
		{
			trace(this, evt.text);
		}
		
		private function onSoundFacadeSecurityError(evt:SecurityErrorEvent):void 
		{
			trace(this, evt.text);
		}
		
		private function onSoundFacadeOpen(evt:Event):void 
		{
			TweenMax.to(seekbar.seek, 0.1, { width:0, onComplete:soundFacade.play } );
			trace(this, evt.type);
		}
		
		private function onSoundFacadeLoadID3(evt:Event):void 
		{
			for (var property:String in evt.target.id3) 
			{ 
				trace(property + ' = ' + evt.target.id3[property]); 
			}
		}
		
		private function onSoundFacadeProgress(evt:ProgressEvent):void 
		{
			var percentage:Number = evt.bytesLoaded / evt.bytesTotal;
			TweenMax.allTo([seekbar.buffer, seekbar.hit], 0.4, { scaleX:percentage } );
		}
		
		private function onSoundFacadePlayProgress(evt:ProgressEvent):void 
		{
			var percentage:Number = evt.bytesLoaded / evt.bytesTotal;
			if (seekbar.buffer.scaleX > percentage)
			{
				TweenMax.to(seekbar.seek, 0.3, { scaleX:percentage, ease:Expo.easeOut } );
			}
			timestamp.text = soundFacade.presentTime + ' / ' + soundFacade.totalTime;
			timestamp.x = (seekbar.x + seekbar.seek.width) - (timestamp.width / 2);
			timestamp.y = (seekbar.y + seekbar.height) + 4;
		}
		
		private function onSoundFacadeComplete(evt:Event):void 
		{
			trace(this, evt.type);
		}
		
		private function onSoundFacadeSoundComplete(evt:Event):void 
		{
			trace(this, evt.type);
		}
		
	}

}